package pcb.views.library 
{
	import com.intel.utils.Debugger;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import pcb.models.vo.FIBAnswer;
	import pcb.views.uievents.SlideEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class FIB extends MovieClip
	{
		private var tf:TextField;
		private var strTfName:String = "txtInput";
		
		private var blnNumeric:Boolean;
		private var blnCaseSensitive:Boolean;
		private var strCorrectAnswer:String;
		
		private var iCharCount:Number;
		private var iLoopCount:Number;
		
		
		public function FIB() 
		{
		
			Debugger.traceOut("NEW FIB created");
		
			this.tf = getTextField(strTfName);
			
			if (this.tf == null) {
				Debugger.traceOut("ERROR could not locate text field", Debugger.ERROR);
			} else {
				this.tf.text = "";
				
				this.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
				stage.focus = tf;

				dispatchEvent(new Event(SlideEvent.INTERACTIVITY_CREATED, true));
			}
			this.addEventListener(Event.REMOVED, removed);
		}
		
		private function removed(e:Event):void 
		{
			removeListeners();
			this.removeEventListener(Event.REMOVED, removed);
		}
		
		internal function removeListeners():void 
		{
			if (this.hasEventListener(KeyboardEvent.KEY_UP)) {
				this.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			}
			if (this.hasEventListener(Event.ENTER_FRAME)) {
				this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}			
			stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}
		
		private function getTextField(in_tf_name:String):TextField
		{
			var ttf:TextField = null;
			if (this.getChildByName(in_tf_name) != null) {				
				ttf = this.getChildByName(in_tf_name) as TextField;
			} else {
				ttf = this.getChildAt(0) as TextField;				
			}
			
			return ttf;
		}
		
		private function keyUpHandler(event:KeyboardEvent):void 
		{			
			if (isCorrectAnswer(tf.text)) {				
				notifyCorrect();
			}
		}
		
		private function notifyCorrect():void
		{
			removeListeners();
			dispatchEvent(new Event(SlideEvent.INTERACTIVITY_COMPLETED, true));
		}
		
		public function disable():void 
		{			
			this.tf.selectable = false;
		}
				
		
		public function setCorrectAnswer(answer:FIBAnswer):void {
			blnNumeric = answer.isNumeric;
			blnCaseSensitive = answer.isCaseSensitive;
			strCorrectAnswer = answer.answer;
		}
		
		private function isCorrectAnswer(strAnswer:String):Boolean {			
			if (strAnswer.length != strCorrectAnswer.length) {
				return false;
			}
			
			if (blnNumeric) {
				if (isNaN(Number(strAnswer))) {
					return false;
				}
				
				if (parseFloat(strCorrectAnswer) == parseFloat(strAnswer)) {
					return true;
				}
			}
			
			if (blnCaseSensitive) {
				if (strAnswer == strCorrectAnswer) {
					return true;
				}
			} else {
				if (strAnswer.toLowerCase() == strCorrectAnswer.toLowerCase()) {
					return true;
				}
			}
			
			return false;
		}
		
		public function addAnnotation(mcAnnotation:AnnotationMC):void 
		{	
			mcAnnotation.name = "mcAnnotation";
			this.parent.addChild(mcAnnotation);
		}
		
		public function getAnnotationMcPosition():DisplayObject {
			return this as DisplayObject;
		}
		
		public function showMe():void 
		{
			if (this.parent.getChildByName("mcAnnotation")) {
				this.parent.removeChild(this.parent.getChildByName("mcAnnotation"));
			}
			iCharCount = 0;
			iLoopCount = 0;
			this.tf.text = "";
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			//removeListeners();
			disable();
		}
		
		private function onEnterFrame(e:Event):void 
		{
			if(iLoopCount % 3 == 0 && iCharCount < strCorrectAnswer.length) {
				this.tf.appendText(strCorrectAnswer.charAt(iCharCount));
				iCharCount++
			}
			
			if(iCharCount == strCorrectAnswer.length) {
				notifyCorrect();				
			}
			iLoopCount++;
		}
		
		
	}
	
}